package exam.controller;


import exam.bean.CompanyInfo;
import exam.service.ICompanyInfoService;
import exam.utils.FileUtil;
import exam.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController()
@RequestMapping("/CompanyInfos")
public class CompanyInfoController {
    @Autowired
    private ICompanyInfoService companyInfoService;

    @Value("${upload.uploadPath}")
    private String uploadPath;

    @PreAuthorize("hasAuthority('sys:sysParam:companyInfo:list')")
    @ResponseBody
    @PostMapping("/searchInfo/{pageNum}/{pageSize}")
    public Msg searchInfo(@PathVariable String pageNum, @PathVariable String pageSize, @RequestBody Map queryMap){
        return Msg.success().add("pageInfo",companyInfoService.selectInfo(Integer.parseInt(pageNum),
                Integer.parseInt(pageSize),queryMap));
    }


    @ResponseBody
    @PreAuthorize("hasAuthority('sys:sysParam:companyInfo:add')")
    @PostMapping("/add")
    public Msg insertInfo(@RequestPart("companyInfo") CompanyInfo companyInfo,
                          @RequestPart("files") MultipartFile[] file){
        companyInfo.setUtilFiles(Arrays.asList(file));
        boolean re = companyInfoService.insertInfo(companyInfo,uploadPath) > 0;
        return re ? Msg.success().setMessage("数据添加成功") :Msg.fail().setMessage("数据添加失败");
    }

    @ResponseBody
    @GetMapping("/detail")
    public Msg detail(@RequestParam(value="id") int id){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id",id);
        return Msg.success().add("companyInfo",companyInfoService.selectInfoSingle(map));
    }

    @ResponseBody
    @PreAuthorize("hasAuthority('sys:sysParam:companyInfo:delete')")
    @GetMapping("/delete")
    public Msg delete (@RequestParam(value="id") int id){
        boolean re = companyInfoService.deleteInfo(id) > 0 ;
        return Msg.success().setMessage(re ? "删除成功":"删除失败");
    }

    @ResponseBody
    @PreAuthorize("hasAuthority('sys:sysParam:companyInfo:update')")
    @PostMapping("/upData")
    public Msg update(@RequestPart("companyInfo") CompanyInfo companyInfo,
                      @RequestPart(value = "files",required = false) MultipartFile[] files){
        companyInfo.setUtilFiles(Arrays.asList(files));
        boolean re = companyInfoService.updateInfo(companyInfo,uploadPath) > 0;
        return re ? Msg.success().setMessage("修改成功"):Msg.fail().setMessage("修改失败");
    }


    @GetMapping("/downLoad")
    public void downLoad(HttpServletResponse response, String fileName, String fileNameSrc){
        FileUtil.downLoadFile(response,uploadPath,fileName,fileNameSrc);
    }


    @ResponseBody
    @GetMapping("/searchInput")
    public Msg searchInput(@RequestParam(value = "companyName",defaultValue = "") String companyName){
        return Msg.success().add("map_companyInfo",companyInfoService.searchInput(companyName));
    }

}
